package com.mc.lp.database.service.handler.user;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import com.google.visualization.datasource.base.TypeMismatchException;
import com.google.visualization.datasource.datatable.ColumnDescription;
import com.google.visualization.datasource.datatable.DataTable;
import com.google.visualization.datasource.datatable.value.ValueType;
import com.mc.lp.core.util.CollectionUtil;
import com.mc.lp.core.util.SortOrder;
import com.mc.lp.database.cache.SubjectCache;
import com.mc.lp.database.entity.type.SchoolGrade;
import com.mc.lp.shared.vo.SearchCriteriaVO;
import com.mc.lp.shared.vo.user.MarksListVO;
import com.mc.lp.shared.vo.user.MarksVO;
/*
 * @author aphanda
 * @date Mar 8, 2015
 * @time 8:03:21 PM
 */

public class UserDashboardHandlerSupport extends UserDashboardHandler{
	
	

	public DataTable generateDataTableForAdvanceGraph(TreeMap<Integer, TreeMap<Long, MarksListVO>> map, SubjectCache subjectCache) {
		// Create a data table.
		DataTable data = new DataTable();
		ArrayList<ColumnDescription> cd = new ArrayList<ColumnDescription>();
		Set<Long> distinctSubject = new HashSet<Long>();
		
		cd.add(new ColumnDescription("Class", ValueType.TEXT, "Class"));

	//	SubjectCache subjectCache = (SubjectCache) getCacheService().getService(CacheServiceType.SUBJECT);
		// Fill the data table.
		try {
			// Creating the Column Description for Charts
			for (Integer gradeId : map.keySet()) {
				TreeMap<Long, MarksListVO> innerMap = map.get(gradeId);
				for (Long subjectId : innerMap.keySet()) {
					distinctSubject.add(subjectId);
				}
			}
			
			for (Long subjectId : distinctSubject) {
				String subjectname = subjectCache.getEntityById(subjectId).getSubjectName();
				cd.add(new ColumnDescription(subjectname, ValueType.NUMBER, subjectname));
			}
			
			data.addColumns(cd);

			// Feeding-in the data for Charts
			for (Integer gradeId : map.keySet()) {
				Set<Long> subjects = distinctSubject;

				// inner map contains subjectId as key and MarksListVO i.e. List of MarksVO (term ,Max Marks, Marks)
				TreeMap<Long, MarksListVO> innerMap = map.get(gradeId);
				String gradeString = SchoolGrade.getSchoolGradeName(gradeId);
				List<Object> yearObjects = new ArrayList<Object>();
				yearObjects.add(gradeString);
				
				Map<String, List<Long>> gradeTermMap = new TreeMap<String, List<Long>>();
				Iterator<Long> iter = subjects.iterator();
				while(iter.hasNext()) {
					Long subjectId  = iter.next();
					MarksListVO vo = innerMap.get(subjectId);
					
					if(vo==null){
						yearObjects.add(0L);
						continue;
					}
					
					// Mapping of Grade-Term as key and subject marks as value list
					Double avgMarks = 0d;
					for (MarksVO marksVo : vo.getMarks()) {
						avgMarks =  (avgMarks  + (float)marksVo.getMarks()/(float)marksVo.getMaxMarks()*100) ;
					}
					avgMarks = avgMarks/vo.getMarks().size();		
					yearObjects.add(avgMarks.longValue());
				}
				
				System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Printing gradeTermMap "+gradeTermMap);
				data.addRowFromValues(yearObjects.toArray(new Object[yearObjects.size()]));
			}
		} catch (TypeMismatchException e) {
			System.out.println("Invalid type!");
		}
		return data;
	}

	
	public DataTable processDataTableForTerm(TreeMap<Integer, TreeMap<Integer, List<MarksVO>>> map,
			SearchCriteriaVO crit, SubjectCache subjectCache) {

		DataTable data = new DataTable();
		ArrayList<ColumnDescription> cd = new ArrayList<ColumnDescription>();
		cd.add(new ColumnDescription("Class", ValueType.TEXT, "Class"));

		//SubjectCache subjectCache = (SubjectCache) getCacheService().getService(CacheServiceType.SUBJECT);
		// Fill the data table.
		try {
			// Creating the Column Description for Charts
			String[] selectedSubjects = crit.getSelectedSubjectList();
			CollectionUtil.sortStringArrayNumerically(selectedSubjects, SortOrder.ASCENDING);
			int noOfSubject = selectedSubjects.length;
			System.out.println("Selected Subjects order by "+selectedSubjects);
			
			if(selectedSubjects!=null){
				for (int i = 0; i < selectedSubjects.length; i++) {
					String subjectname = subjectCache.getEntityById(Long.valueOf(selectedSubjects[i])).getSubjectName();
					cd.add(new ColumnDescription(subjectname, ValueType.NUMBER, subjectname));
				}
			}
			data.addColumns(cd);
			// Feeding-in the data for Charts
			for (Integer gradeId : map.keySet()) {
				TreeMap<Integer, List<MarksVO>> innerMap = map.get(gradeId);
				String gradeString = SchoolGrade.getSchoolGradeName(gradeId);
				//List<Object> termObjects = new ArrayList<Object>();
				for (Integer term : innerMap.keySet()) {
					List<Object> objects= new ArrayList<Object>();
					objects.add(gradeString+"/T-"+term);
					
					List<MarksVO> marks = new ArrayList<MarksVO>();
					// get all the marks of the Term 
					marks = innerMap.get(term);
					
					for (int i = 0; i < selectedSubjects.length; i++) {
						String selectedSubject = selectedSubjects[i];
						boolean subjectAdded = false;
						for (MarksVO marksVO : marks) {
							if(marksVO.getSubjectId().toString().equals(selectedSubject)){
								objects.add(marksVO.getMarks());
								subjectAdded = true;
								break;
							}
						}
						if(!subjectAdded){
							objects.add(0L);
						}
					}
					// adding data in the table 
					data.addRowFromValues(objects.toArray(new Object[objects.size()]));
				}
			}
		} catch (Exception e) {
			// serviceResponse :P
			System.out.println("Invalid type!");
		}
		return data;

	}
	
	
}
